if (typeof jQuery !== 'undefined') {
        (function($) {
                $('#spinner').ajaxStart(function() {
                        $(this).fadeIn();
                }).ajaxStop(function() {
                        $(this).fadeOut();
                });
        })(jQuery);
}

window.onload = function() {
    $(".autoclick").click();
    connect();
}

function update(){
	$('#reloadExchangePanel').click();
	$('#reloadHistory').click();
	$('#reloadPlayerInfo').click();
	$('#reloadCardHistory').click();
	$('#reloadTurnStatusPanel').click();
}

function connect(){
	this.timeoutID = setTimeout(function(){
		update();
		// recurse
		connect();
	  }, 5000);
	}

function disconnect(){
	if (typeof this.timeoutID == "number") {
		window.clearTimeout(this.timeoutID);
		delete this.timeoutID;
	}
}

function incrementValue(inc,row){
	var inc = parseInt(inc);
	var diffElem = $("#diff-"+row);
	var sharePriceElem = $("#sharePrice-"+row);
	var countOldElem = $("#countOld-"+row);
	var countNewElem = $("#countNew-"+row);
	
	var diffValue =  parseInt(diffElem.attr("value"));
	var sharePrice = parseInt(sharePriceElem.html());
	var countOld = parseInt(countOldElem.html());
	var countNew = parseInt(countNewElem.html());

	if(isNaN(diffValue)){
		diffValue = 0;
	}
	diffValue = diffValue + inc;
	countNew = countOld + diffValue;
	countNewElem.text(countNew);
	diffElem.attr("value",diffValue);
	
	var valid = (countNew >= 0)
	// validation countNew not negative
	if(valid){
		countNewElem.removeClass("validationError");
	}else{
		countNewElem.addClass("validationError");
	}
	var cashValid = updateNewCashValue();

	if(valid && cashValid){
		$("#tradeBtn").removeAttr('disabled');
	}else{
		$("#tradeBtn").attr('disabled', 'disabled');
	}
	
	
	return false;
}

function updateNewCashValue(){
	var cashOldElem = $("#cashOld");
	var cashNewElem = $("#cashNew");
	
	var cashOld = parseInt(cashOldElem.html());
	var cashNew = parseInt(cashNewElem.html());

	//TODO calculation of cashNew is wrong
	cashNew = cashOld;
	$('.shareRow').each(function( index ) {
		var id = $(this).attr('data-id');
		var diff = parseInt($('#diff-'+id).attr("value"));
		var price = parseInt($('#sharePrice-'+id).html());
		cashNew = cashNew - (diff * price);
	});
	
	cashNewElem.text(cashNew);
	
	var valid = (cashNew >= 0)
	// validation cashNew not negative
	if(valid){
		cashNewElem.removeClass("validationError");
	}else{
		cashNewElem.addClass("validationError");
	}
	
	return valid;
}
//
//function initPopover(){
//	$('a.remotePopover').popover({
//	    "html": true,
//	    "content": function()
//	    {
//	        var div_id =  "div-id-" + $.now();
//	        return details_in_popup($(this).attr('href'), div_id)
//	        return div_id;
//    }});
//}
//
//function details_in_popup(link, div_id){
//	$.ajax({
//	    url: link,
//	    success: function(response){
//	        $('#'+div_id).html(response)}});
//	return '<div id="'+ div_id +'">Loading...</div>'
//}